package com.mobileseed.weather.data;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class WeatherStringParse {
	public static Weather parse(String googleWeatherString){		
		Weather weather = new Weather();
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		
		XMLReader xmlReader;
		try {
			xmlReader = saxParserFactory.newSAXParser().getXMLReader();
			WeatherXMLHandler handler = new WeatherXMLHandler(weather);
			xmlReader.setContentHandler(handler);			
			xmlReader.parse(new InputSource(new StringReader(googleWeatherString)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}catch(IOException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		//检查返回是否合法
		if(weather.containsValidInformation())
		{
			return weather;
		}
		else
		{
			return null;
		}
	}
}
